package com.cn.intelligence.resetful.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cn.intelligence.mapper.TbItemCatMapper;
import com.cn.intelligence.mapper.TbItemDescMapper;
import com.cn.intelligence.mapper.TbItemMapper;
import com.cn.intelligence.mapper.TbItemParamItemMapper;
import com.cn.intelligence.pojo.TaotaoResult;
import com.cn.intelligence.pojo.TbItem;
import com.cn.intelligence.pojo.TbItemCat;
import com.cn.intelligence.pojo.TbItemCatExample;
import com.cn.intelligence.pojo.TbItemDesc;
import com.cn.intelligence.pojo.TbItemDescExample;
import com.cn.intelligence.pojo.TbItemExample;
import com.cn.intelligence.pojo.TbItemParamItem;
import com.cn.intelligence.pojo.TbItemParamItemExample;
import com.cn.intelligence.resetful.pojo.Conts;
import com.cn.intelligence.resetful.pojo.ItemDesc;
import com.cn.intelligence.resetful.service.ItemService;
import com.github.pagehelper.PageHelper;
@Service
public class ItemServiceImpl implements ItemService{
	@Autowired
	TbItemMapper tbItemMapper;
	@Autowired
	TbItemDescMapper tbItemDescMapper;
	@Autowired
	TbItemParamItemMapper tbItemParamItemMapper;
	@Autowired
	TbItemCatMapper catMapper;

	/**
	 * 获取商品详细情况
	 */
	@Override
	public TaotaoResult selectProductDesc(Long id) {
		// 查询基本信息
		TbItem tbitem = tbItemMapper.selectByPrimaryKey(id);
		// 查询详细描述
		TbItemDescExample tbItemDescExample = new TbItemDescExample();
		TbItemDescExample.Criteria criItemDesc = tbItemDescExample.createCriteria();
		criItemDesc.andItemIdEqualTo(id);
		List<TbItemDesc> descList= tbItemDescMapper.selectByExampleWithBLOBs(tbItemDescExample);
		// 查询规格参数
		TbItemParamItemExample tbItemParamItemExample = new TbItemParamItemExample();
		TbItemParamItemExample.Criteria criItemparamItem = tbItemParamItemExample.createCriteria();
		criItemparamItem.andItemIdEqualTo(id);
		List<TbItemParamItem> paramList= tbItemParamItemMapper.selectByExampleWithBLOBs(tbItemParamItemExample);
		// 查询商品分类
		TbItemCatExample catExample = new TbItemCatExample();
		TbItemCatExample.Criteria catItem = catExample.createCriteria();
		catItem.andIdEqualTo(tbitem.getCid());
		List<TbItemCat> catList= catMapper.selectByExample(catExample);
		// 查询上级分类
		TbItemCatExample catExample2 = new TbItemCatExample();
		TbItemCatExample.Criteria catItem2 = catExample2.createCriteria();
		catItem2.andIdEqualTo(catList.get(0).getParentId());
		List<TbItemCat> catList2= catMapper.selectByExample(catExample2);
		
		ItemDesc itemdesc = new ItemDesc();
		itemdesc.setId(Long.toString(tbitem.getId()));
		itemdesc.setBrand_id(Long.toString(catList.get(0).getId()));
		itemdesc.setParams(paramList.get(0).getParamData());
		itemdesc.setName(tbitem.getName());
		itemdesc.setIntro(tbitem.getIntro());
		itemdesc.setVideo(tbitem.getVideo());
		itemdesc.setPro_number(tbitem.getBarcode());
		itemdesc.setPrice(tbitem.getSprice());
		itemdesc.setPrice_yh(tbitem.getBprice());
		itemdesc.setPhoto_d(tbitem.getPhotoX().indexOf(",")<0?tbitem.getPhotoX():tbitem.getPhotoX().split(",")[0]);
		itemdesc.setContent(descList.get(0).getItemDesc());
		itemdesc.setAddtime(tbitem.getCreated());
		itemdesc.setUpdatetime(tbitem.getUpdated());
		itemdesc.setUpdatetime(tbitem.getUpdated());
		itemdesc.setShiyong(Integer.toString(tbitem.getShiyong()==null?0:tbitem.getShiyong()));
		itemdesc.setNum(Integer.toString(tbitem.getNum()==null?0:tbitem.getNum()));
		itemdesc.setType(Byte.toString(tbitem.getStatus()));
		itemdesc.setZhongliang(tbitem.getZhongliang());
		itemdesc.setCaizhi(tbitem.getCaizhi());
		itemdesc.setPinpai(tbitem.getPinpai());
		itemdesc.setCid(Long.toString(catList2.get(0).getId()));
		itemdesc.setCompany(tbitem.getUnit());
		itemdesc.setIs_hot(Integer.toString(tbitem.getIsHot()));
		itemdesc.setIs_show(Integer.toString(tbitem.getIsShow()));
		itemdesc.setBrand(catList.get(0).getName());
		itemdesc.setCat_name(catList2.get(0).getName());
		itemdesc.setImg_arr(tbitem.getPhotoX().indexOf(",")<0?new String[]{tbitem.getPhotoX()}:tbitem.getPhotoX().split(","));
		itemdesc.setPhoto_x(itemdesc.getImg_arr()[0]);
		itemdesc.setCollect("0");
		Map<String,Object> pro = new HashMap<String,Object>();
		pro.put("pro", itemdesc);
		pro.put("commodityAttr", null);
		pro.put("attrValueList", null);
		return TaotaoResult.ok(pro);
	}
	/**
	 * 加载商品分类详情
	 */
	@Override
	public TaotaoResult getCategory(Long catId) {
		Map<String,Object> pro = new HashMap<String,Object>();
		
		if(catId !=null && !"".equals(catId)){
			// 查询一级标题
			List<TbItemCat> listcat = getItTemCat(catId);
			pro.put("catList", listcat);
			return TaotaoResult.ok(pro);
		}else{
			// 查询一级标题
			List<TbItemCat> listcat = getItTemCat(catId);
			pro.put("list", listcat);
			// 查询二级标题
			List<TbItemCat> listcatLevel2 = getItTemCat(listcat.get(0).getId());
			pro.put("list", listcat);
			pro.put("catList", listcatLevel2);
			return TaotaoResult.ok(pro);
		}


	}
	
	private List<TbItemCat>  getItTemCat(Long parentCatId){
		// 查询商品分类
		TbItemCatExample catExample = new TbItemCatExample();
		TbItemCatExample.Criteria catItem = catExample.createCriteria();
		if(parentCatId !=null && !"".equals(parentCatId)){
			catItem.andIsParentEqualTo(false);
			catItem.andParentIdEqualTo(parentCatId);
		}else{
			catItem.andIsParentEqualTo(true);
		}
		List<TbItemCat> listcat = catMapper.selectByExample(catExample);
		return listcat;
	}
	/**
	 * 根据商品分类查询商品信息
	 */
	@Override
	public TaotaoResult selectItemBycategory(Long catId, Long brandId, String ptype,int page) {
		
		// 根据brandid查询商品分类ID
		
		// 根据商品分类ID查询商品信息
		TbItemExample itemExample = new TbItemExample();
		TbItemExample.Criteria itemcriteria = itemExample.createCriteria();
		itemcriteria.andCidEqualTo(catId);
		itemExample.setOrderByClause("created DESC, updated DESC");
		itemcriteria.andStatusNotEqualTo((byte)2);
	       // 分页查询
		if(page>0){
        PageHelper.startPage(page,Conts.ROWS);
		}
		List<TbItem> list = tbItemMapper.selectByExampleWithBLOBs(itemExample);
		List<TbItem> listresult = new ArrayList<TbItem>(); 
		for (TbItem tbItem : list) {
			tbItem.setPhotoX((tbItem.getPhotoX()!=null && !"".equals(tbItem.getPhotoX()))?tbItem.getPhotoX().split(",")[0]:"");
			listresult.add(tbItem);
		}
		return TaotaoResult.ok(listresult);
	}

}
